package com.tsmti.lab.dao.impl;

import com.tsmti.core.common.dao.impl.BaseDaoImpl;
import com.tsmti.core.common.query.data.Queryable;
import com.tsmti.core.util.BeanTransformerAdapter;
import com.tsmti.core.util.StringUtils;
import com.tsmti.lab.dao.EmbedDao;
import com.tsmti.lab.dto.PimsPathologyPiecesView;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Package：com.tsmti.lab.dao.impl
 * @Class：EmbedDaoImpl
 * @Description： TODO
 * @Author：Shiyan.shi
 * @Date：Created in 2018/11/17 下午3:10
 * @Company:
 * @Version：
 * @Modified By:
 */
@Repository("embedDao")
public class EmbedDaoImpl extends BaseDaoImpl implements EmbedDao {
    /**
     * 包埋页面患者列表总数
     * @param pimsPathologyPiecesView
     * @return
     */
    @Override
    public PimsPathologyPiecesView getDetialInfo(Long id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select PIECES.piePathologyCode as piePathologyCode, PIECES.pieCode as pieCode ,PIECES.pieCounts as pieCounts,PIECES.pieNullSlideNum as pieNullSlideNum,PIECES.pieParts as pieParts ," +
                " PIECES.pieDoctorName as pieDoctorName," +
                " PIECES.pieSamplingTime as pieSamplingTime , PIECES.pieEmbedDoctorName as pieEmbedDoctorName , PIECES.pieEmbedTime as pieEmbedTime , PIECES.pieDoctorName as pieDoctorName , " +
                " SAMPLE.samPatientName as samPatientName ,SAMPLE.samPatientNumber as samPatientNumber ,SAMPLE.samSendUserName as samSendUserName " +
                "  ,SAMPLE.samSendHospitalName as samSendHospitalName , SAMPLE.samSendDeptName as samSendDeptName , SAMPLE.samPatientBed as samPatientBed " +
                "  ,SAMPLE.samPatientAge as samPatientAge,SAMPLE.samPatientSex as samPatientSex , SAMPLE.samSampleName as samSampleName , SAMPLE.samOpsShow as samOpsShow " +
                " ,SAMPLE.samDignoses as samDignoses from PimsPathologyPieces  PIECES , PathologySample  SAMPLE where PIECES.pieSampleId = SAMPLE.id and  PIECES.id = ?");
        //List<ManyToVo> list = query.setResultTransformer(Transformers.aliasToBean(ManyToVo.class)).list();
        //List<PimsPathologyPiecesView> list = findByHql(PimsPathologyPiecesView.class,sql.toString(),id);
        //Query query = getSession().createQuery(sql.toString());
        //query.setParameter(0,id);
        //List<PimsPathologyPiecesView> list = query.setResultTransformer(Transformers.aliasToBean(PimsPathologyPiecesView.class)).list();
        List<PimsPathologyPiecesView> list =findByHqls(PimsPathologyPiecesView.class,sql.toString(),id);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int getListTotal(PimsPathologyPiecesView pimsPathologyPiecesView) {
        StringBuffer sql = new StringBuffer();
        sql.append("select count(*) from PIMS_PATHOLOGY_PIECES  pieces , PIMS_PATHOLOGY_SAMPLE  sample where PIECES.PIESAMPLEID = SAMPLE.ID ");
        getListSql(sql, pimsPathologyPiecesView);
        return countSqlTotal(sql.toString());
    }

    /**
     * 包埋页面患者列表
     * @param pimsPathologyPiecesView
     * @param queryable
     * @return
     */
    @Override
    public List<PimsPathologyPiecesView> getList(PimsPathologyPiecesView pimsPathologyPiecesView, Queryable queryable) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select PIECES.*,  SAMPLE.SAMPATIENTNAME as samPatientName ,SAMPLE.SAMPATIENTNUMBER samPatientNumber from PIMS_PATHOLOGY_PIECES  pieces , PIMS_PATHOLOGY_SAMPLE  sample where PIECES.PIESAMPLEID = SAMPLE.ID ");
        getListSql(sql, pimsPathologyPiecesView);
        sql = new StringBuffer(sortByQueryable(sql.toString(), queryable));
        Query query = getSession().createSQLQuery(sql.toString()).setResultTransformer((new BeanTransformerAdapter(PimsPathologyPiecesView.class)));
        query.setFirstResult(getStartPageNum(queryable));
        query.setMaxResults(getEndPageNum(queryable));
        return query.list();
    }

    private StringBuffer getListSql(StringBuffer sql, PimsPathologyPiecesView pimsPathologyPiecesView) {
        //患者姓名
        if (StringUtils.isNotEmpty(pimsPathologyPiecesView.getSamPatientName())) {
            sql.append(" and SAMPLE.SAMPATIENTNAME like '%" + pimsPathologyPiecesView.getSamPatientName() + "%'");
        }
        //门诊住院号
        if (StringUtils.isNotEmpty(pimsPathologyPiecesView.getSamPatientNumber())) {
            sql.append(" and SAMPLE.SAMPATIENTNUMBER like '%" + pimsPathologyPiecesView.getSamPatientNumber()+"%'");
        }
        //取材医生
        if (StringUtils.isNotEmpty(pimsPathologyPiecesView.getPieDoctorName())) {
            sql.append(" and pieces.PIEDOCTORNAME like '%" + pimsPathologyPiecesView.getPieDoctorName()+"%'");
        }
        //患者类型(病人类型： 1门诊,2住院,3体检,4婚检,5科研,6特勤,7其他)   在院区分
        if (pimsPathologyPiecesView.getSamPatientType()!=null && pimsPathologyPiecesView.getSamPatientType() >=1 && pimsPathologyPiecesView.getSamPatientType() <=7) {
            sql.append(" and SAMPLE.SAMPATIENTTYPE = " + pimsPathologyPiecesView.getSamPatientType());
        }
        //病理编号
        if (StringUtils.isNotEmpty(pimsPathologyPiecesView.getStartPiePathologyCode()) && StringUtils.isNotEmpty(pimsPathologyPiecesView.getEndPiePathologyCode())) {
            sql.append(" and SAMPLE.SAMPATHOLOGYCODE between '" + pimsPathologyPiecesView.getStartPiePathologyCode() + "' and '" + pimsPathologyPiecesView.getEndPiePathologyCode()+"'");
        }
//        if (StringUtils.isNotEmpty(pimsPathologyPiecesView.getEndPiePathologyCode())) {
//            sql.append(" and SAMPLE.SAMPIEPATHOLOGYCODE < " + pimsPathologyPiecesView.getEndPiePathologyCode() );
//        }
        //取材日期
        if (pimsPathologyPiecesView.getStartPieSamplingTime()!=null ) {
//            sql.append(" and SAMPLE.CREATEDATE > to_date('" + pimsPathologyPiecesView.getStartPieSamplingTime() + "','YYYY-MM-DD HH24:MI:SS')");
        }
        if (pimsPathologyPiecesView.getEndPieSamplingTime()!=null ) {
//            sql.append(" and SAMPLE.CREATEDATE < to_date('" + pimsPathologyPiecesView.getEndPieSamplingTime() + "','YYYY-MM-DD HH24:MI:SS')");
        }
        //包埋状态
        if (!StringUtils.isEmpty(pimsPathologyPiecesView.getPieState())) {
            sql.append(" and pieces.PIESTATE =" + pimsPathologyPiecesView.getPieDoctorName());
        }
        return sql;
    }
}
